package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.entity.dto.BrandDto;
import cn.edu.lingnan.mapper.BrandMapper;
import cn.edu.lingnan.service.BrandService;
import cn.edu.lingnan.entity.pojo.Brand;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Override
    public Page<Brand> getBrandsByPage(BrandDto brandDto) {
        // 创建分页对象
        Page<Brand> page = new Page<>(brandDto.getPageNum(), brandDto.getPageSize());

        // 创建查询条件
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();

        // 添加模糊查询条件
        if (StringUtils.isNotBlank(brandDto.getBrandName())) {
            queryWrapper.like(Brand::getBrandName, brandDto.getBrandName());
        }
        if (StringUtils.isNotBlank(brandDto.getDesc())) {
            queryWrapper.like(Brand::getBrandDesc, brandDto.getDesc());
        }

        // 执行分页查询
        Page<Brand> resultPage = baseMapper.selectPage(page, queryWrapper);

        // 替换 logoUrl 中的反斜杠
        resultPage.getRecords().forEach(brand -> {
            if (brand.getLogoUrl() != null) {
                brand.setLogoUrl(brand.getLogoUrl().replace("\\", "/"));
            }
        });

        return resultPage;
    }

}